package leetcode;

import java.util.Stack;

public class Leetcode020 {

    public boolean isValid(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        Stack<Character> stack = new Stack<Character>();
        char[] chars = s.toCharArray();
        char prev;
        for (char cha: chars) {
            if (cha == '{') {
                stack.push('}');
            }else if (cha == '[') {
                stack.push(']');
            }else if (cha == '(') {
                stack.push(')');
            }else if (stack.isEmpty() || stack.pop() != cha) {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
